package com.estun.backroundManagementService.modules.material.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.material.dto.request.QrCodeRequest;
import com.estun.backroundManagementService.modules.material.model.MaterialBatch;
import com.estun.backroundManagementService.modules.material.service.MaterialBatchService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/materialBatch")
@AllArgsConstructor
public class MaterialBatchController {

    private final MaterialBatchService materialBatchService;

    //分页查询物料信息
    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<MaterialBatch> pageRequest) {
        Page<MaterialBatch> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<MaterialBatch> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<MaterialBatch> page1 = materialBatchService.page(page, queryWrapper);
        List<MaterialBatch> list = materialBatchService.list();
        return BaseResponse.success(page1);
    }

    // 查询所有
    @GetMapping("/all")
    public BaseResponse getAllMaterialBatches() {
        return BaseResponse.success(materialBatchService.list());
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public BaseResponse getMaterialBatchById(@PathVariable Integer id) {
        MaterialBatch materialBatch = materialBatchService.getById(id);
        return BaseResponse.success(materialBatch);
    }

    // 手动上料
    @PostMapping("/add")
    @Log(module = "物料批次管理 手动上料", type = LogConstants.INSERT, desc = "物料批次管理 手动上料")
    public BaseResponse addMaterialBatch(@RequestBody MaterialBatch materialBatch) {
        return materialBatchService.addMaterialBatch(materialBatch);
    }

    // 扫码上料
    @PostMapping("/autoAdd")
    @Log(module = "物料批次管理 扫码上料", type = LogConstants.INSERT, desc = "物料批次管理 扫码上料")
    public BaseResponse autoAdd(@RequestBody QrCodeRequest materialBatch) {
        return materialBatchService.autoAdd(materialBatch);
    }

    // 更新
    @PutMapping("/update")
    @Log(module = "物料批次管理 更新", type = LogConstants.UPDATE, desc = "物料批次管理 更新")
    public BaseResponse updateMaterialBatch(@RequestBody MaterialBatch materialBatch) {
        materialBatchService.updateById(materialBatch);
        return BaseResponse.success("更新成功");
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    @Log(module = "物料批次管理 删除", type = LogConstants.DELETE, desc = "物料批次管理 删除")
    public BaseResponse deleteMaterialBatch(@PathVariable Integer id) {
        materialBatchService.removeById(id);
        return BaseResponse.success("删除成功");
    }
}
